﻿namespace Pub.Class
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Text;

    [SuppressUnmanagedCodeSecurity]
    internal class WinApiProvider
    {
        public const int SC_CLOSE = 0xf060;
        private const int WM_SETTEXT = 12;
        public const uint WM_SYSCOMMAND = 0x112;

        [DllImport("gdi32.dll ")]
        public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        [DllImport("user32.dll")]
        public static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
        public static List<IntPtr> EnumChildWindows(IntPtr hParentWnd)
        {
            List<IntPtr> list = new List<IntPtr>();
            GCHandle handle = GCHandle.Alloc(list);
            try
            {
                EnumWindowProc callback = new EnumWindowProc(WinApiProvider.EnumWindow);
                EnumChildWindows(hParentWnd, callback, GCHandle.ToIntPtr(handle));
            }
            finally
            {
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
            }
            return list;
        }

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32")]
        private static extern bool EnumChildWindows(IntPtr hWnd, EnumWindowProc callback, IntPtr userObject);
        private static bool EnumWindow(IntPtr hChildWindow, IntPtr pointer)
        {
            ((List<IntPtr>) GCHandle.FromIntPtr(pointer).Target).Add(hChildWindow);
            return true;
        }

        [DllImport("user32", CharSet=CharSet.Auto)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        public static IntPtr GetChildHandle(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass)
        {
            return FindWindowEx(hwndParent, hwndChildAfter, lpszClass, null);
        }

        public static List<IntPtr> GetChildHandles(IntPtr hwndParent, string className)
        {
            List<IntPtr> list = new List<IntPtr>();
            for (IntPtr ptr = GetChildHandle(hwndParent, IntPtr.Zero, className); ptr != IntPtr.Zero; ptr = GetChildHandle(hwndParent, ptr, className))
            {
                list.Add(ptr);
            }
            return list;
        }

        public static string GetClassName(IntPtr hWnd)
        {
            StringBuilder lpClassName = new StringBuilder(0x80);
            if (GetClassName(hWnd, lpClassName, lpClassName.Capacity) == 0)
            {
                throw new Exception("not found IntPtr!");
            }
            return lpClassName.ToString();
        }

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
        public static Rectangle GetClientRect(IntPtr hWnd)
        {
            Rectangle rc = new Rectangle();
            GetClientRect(hWnd, ref rc);
            return rc;
        }

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int GetClientRect(IntPtr hwnd, ref Rectangle rc);
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out Point pt);
        public static Point GetCursorPosPoint()
        {
            Point pt = new Point();
            if (GetCursorPos(out pt))
            {
                return pt;
            }
            return new Point();
        }

        public static IntPtr GetHandle(Point p)
        {
            return WindowFromPoint(p);
        }

        public static IntPtr GetHandle(string IpClassName)
        {
            return FindWindow(IpClassName, null);
        }

        [DllImport("user32.dll")]
        public static extern IntPtr GetParent(IntPtr hWnd);
        public static string GetText(IntPtr hWnd)
        {
            StringBuilder lpString = new StringBuilder(0x80);
            GetWindowText(hWnd, lpString, lpString.Capacity);
            return lpString.ToString();
        }

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        public static List<string> GetWindowNames(List<IntPtr> windowHandles)
        {
            List<string> list = new List<string>();
            StringBuilder lpString = new StringBuilder(260);
            foreach (IntPtr ptr in windowHandles)
            {
                GetWindowText(ptr, lpString, 260);
                list.Add(lpString.ToString());
            }
            return list;
        }

        public static Rectangle GetWindowRect(IntPtr hWnd)
        {
            Rectangle rc = new Rectangle();
            GetWindowRect(hWnd, ref rc);
            return rc;
        }

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int GetWindowRect(IntPtr hwnd, ref Rectangle rc);
        [DllImport("user32", CharSet=CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        public static Rectangle ScreenToClient(IntPtr hWnd, Rectangle rect)
        {
            Rectangle rectangle = rect;
            ScreenToClient(hWnd, ref rectangle);
            return rectangle;
        }

        [DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)]
        public static extern int ScreenToClient(IntPtr hWnd, ref Rectangle rect);
        [DllImport("user32", CharSet=CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref CopyDataStruct lParam);
        [DllImport("user32", CharSet=CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref PARAFORMAT2 lParam);
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
        [DllImport("user32")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        public static extern IntPtr SetActiveWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern void SetForegroundWindow(IntPtr hwnd);
        [DllImport("user32")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        public static void SetText(IntPtr hWnd, string lParam)
        {
            SendMessage(hWnd, 12, IntPtr.Zero, lParam);
        }

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern long SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("shell32.dll", EntryPoint="ShellExecute")]
        public static extern int ShellExecuteA(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
        [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
        public static extern IntPtr WindowFromPoint(Point pt);

        public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
    }
}

